package com.guofen.service.hsmpy.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.HsmSmallWormMapper;
import com.guofen.pojo.HsmSmallWorm;
import com.guofen.pojo.SjqFlocculationCraftScene;
import com.guofen.service.hsmpy.HatchingFeedService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HatchingFeedServiceImpl implements HatchingFeedService {

    @Autowired
    private HsmSmallWormMapper hsmSmallWormMapper;

    @Override
    public boolean add(String hatch_start_time, String batch, String worm_group, String hatch_box_number,
                       String ferment_material_type, Double ferment_material_w, String fuliao_name, Double fuliao_w, Double chongluanfuliao_w,
                       Double water_w, Double egg_w, Double clfpslbw_w, String remark) {
        String date = DateAndTimeUtil.getDate();

        HsmSmallWorm hsmSmallWorm = new HsmSmallWorm();
        hsmSmallWorm.setDate(date);
        hsmSmallWorm.setHatchStartTime(hatch_start_time);
        hsmSmallWorm.setBatch(batch);
        hsmSmallWorm.setWormGroup(worm_group);
        hsmSmallWorm.setHatchBoxNumber(hatch_box_number);
//        hsmSmallWorm.setHatchBoxWeight(hatch_box_w);
        hsmSmallWorm.setFermentMaterialType(ferment_material_type);
        hsmSmallWorm.setFermentMaterialWeight(ferment_material_w);
        hsmSmallWorm.setFuliaoName(fuliao_name);
        hsmSmallWorm.setFuliaoWeight(fuliao_w);
        hsmSmallWorm.setChongluanfuliaoWeight(chongluanfuliao_w);
        hsmSmallWorm.setWaterWeight(water_w);
        hsmSmallWorm.setEggWeight(egg_w);
        hsmSmallWorm.setClfpslbwWeight(clfpslbw_w);
        hsmSmallWorm.setRemark(remark);
        int result = hsmSmallWormMapper.insert(hsmSmallWorm);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public List<Map<String, HsmSmallWorm>> getlatest() {
        return hsmSmallWormMapper.getHatchingFeedLatest();
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<HsmSmallWorm> list = hsmSmallWormMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<HsmSmallWorm> list = hsmSmallWormMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return hsmSmallWormMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(HsmSmallWorm record) {
        return hsmSmallWormMapper.updateByPrimaryKeySelective(record);
    }
}
